package com.demo;

import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.View;

public class PlaneActivity extends Activity {
	
	private int speed = 10;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//去掉窗口标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//全屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		final com.demo.PlaneView pv = new com.demo.PlaneView(this);
		setContentView(pv);
		
		pv.setBackgroundResource(R.drawable.back);
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		DisplayMetrics metrics = new DisplayMetrics();
		//获取屏幕的宽和高
		display.getMetrics(metrics);
		pv.currentX = metrics.widthPixels / 2;
		pv.currentY = metrics.heightPixels - 40;
		pv.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				pv.currentX = event.getRawX();
				pv.currentY = event.getRawY();
				pv.invalidate();
				return true;
			}
		});
		pv.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View source, int keyCode, KeyEvent event)
			{
				switch (event.getKeyCode())
				{
					//下移
					case KeyEvent.KEYCODE_S:
						pv.currentY += speed;
						break;
					case KeyEvent.KEYCODE_W:
						pv.currentY -= speed;
						break;
					case KeyEvent.KEYCODE_A:
						pv.currentX -= speed;
						break;
					case KeyEvent.KEYCODE_D:
						pv.currentX += speed;
						break;
				}
				//planeview组件重绘
				pv.invalidate();
				return true;
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.plane, menu);
		return true;
	}

}
